//
//  MSTool.m
//  MSDemo
//
//  Created by mac on 2021/12/15.
//

#import "MSTool.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>

@implementation MSTool

void ms_showHud(UIView * __nullable view, NSString *text, BOOL autoHide)
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo: view ?: ms_window() animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    if (ms_stringIsEmpty(text))
    {
        hud.mode = MBProgressHUDModeCustomView;
    }
    else
    {
        hud.mode = MBProgressHUDModeText;
        hud.label.numberOfLines = 0;
    }
    if (autoHide)
    {
        [hud hideAnimated:YES afterDelay:1.5];
    }
}
void ms_hideHud(UIView * __nullable view)
{
    [MBProgressHUD hideHUDForView:view ?: ms_window() animated:YES];
}


#pragma mark - window
/** window */
UIWindow* ms_window(void)
{
    UIWindow *window = nil;
    if (@available(iOS 13.0, *))
    {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes)
        {
            if (windowScene.activationState == UISceneActivationStateForegroundActive)
            {
                window = windowScene.windows.firstObject;
                break;
            }
        }
    }
    
    if (!window)
    {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0f)
        {
            window = [[[UIApplication sharedApplication] delegate] window];
        }
        else
        {
            window = [UIApplication sharedApplication].keyWindow;
            if (!window)
            {
                window = [[[UIApplication sharedApplication] delegate] window];
            }
        }
        
    }
    
    return window;
}


#pragma mark - 移除按钮 target
/** 移除按钮点击事件 */
void ms_removeTargetsOfButton(UIButton *button)
{
    NSSet *targets = [button allTargets];
    NSEnumerator *enumerator = [targets objectEnumerator];
    NSObject *obj = nil;
    while (obj = [enumerator nextObject])
    {
        [button removeTarget:obj
                      action:nil
            forControlEvents:UIControlEventTouchUpInside];
    }
}


/** json转model */
void ms_propertyModeWithDic(id jsonString)
{
        
    id obj = [jsonString ms_JSONObject];
    NSDictionary *dict;
    if ([obj isKindOfClass:[NSDictionary class]])
    {
        
        dict = obj;
    }
    else if ([obj isKindOfClass:[NSArray class]])
    {
        
        dict = obj[0];
    }
    else
    {
        
        dict = [NSDictionary dictionary];
    }
    
    NSMutableString *strM = [NSMutableString string];
    
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        
        
        NSString *str;
        
        if ([NSStringFromClass([obj class]) containsString:@"String"])
        {
           str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, copy) NSString *%@;", key];
        }
        
        if ([NSStringFromClass([obj class]) containsString:@"Number"])
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, strong) NSNumber *%@;", key];
        }
        
        if ([NSStringFromClass([obj class]) containsString:@"Array"])
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, copy) NSArray *%@;", key];
        }
        
        if ([NSStringFromClass([obj class]) containsString:@"Dictionary"])
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, copy) NSDictionary *%@;", key];
        }
        
        if ([NSStringFromClass([obj class]) containsString:@"Boolean"])
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, assign) BOOL %@;", key];
        }
        
        [strM appendFormat:@"\n%@",str];
    }];
    
    MSLog(@"\n\n\n=======自动生成属性声明的代码=======\n%@\n\n",strM);
}
/** 获取idfa */
NSString *ms_idfa(void)
{
    //https://blog.csdn.net/lwb102063/article/details/114938899
    //Info.plist NSUserTrackingUsageDescription
    __block NSString *idfa = @"";
    if (@available(iOS 14, *))
    {
        // iOS14及以上版本需要先请求权限
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
           
            if (status == ATTrackingManagerAuthorizationStatusAuthorized)
            {
                idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
            }
            else
            {
                MSLog(@"请在设置-隐私-跟踪中允许App请求跟踪");
            }
        }];
    }
    else
    {
        // iOS14一下版本依然使用老方法
        // 判断在设置-隐私里用户是否打开了广告跟踪
        if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled])
        {
            idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
        }
        else
        {
            MSLog(@"请在设置-隐私-广告中打开广告跟踪功能");
        }
    }
    
    MSLog(@"idfa = %@", idfa);
    return idfa;
}


@end
